/**
 * Copyright (C) 2011 The Android Open Source Project 
 * Emial : guoyil199@163.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package com.json;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;


/**
 * @author THINK
 * @Email guoyil199@163.com
 * @version V1.0
 * @Date 2016年6月28日 上午10:35:02
 * @Title <B>todo</B>
 * 
 *        <pre>
 *        todo
 * 		</pre>
 */

public class Main {

	static String json = "{\"errCode\":0,\"errMsg2\":\"Test\",\"routes\":[{\"seq\":1,\"x\":2188,\"y\":136},{\"seq\":2,\"x\":2149,\"y\":161}]}";
	static Gson gson=new Gson();
	
	
	public static void main(String[] args) {
		Result<Routes>result=parse(Routes.class);
		System.out.println(result.errCode);
		System.out.println(result.errMsg);
		//System.out.println(result.t);
		@SuppressWarnings("unchecked")
		List<Routes> list=(List<Routes>)result.t;
		for (Routes routes : list) {
			System.out.println(routes.toString());
		}
	}

	
	@SuppressWarnings("unchecked")
	public static <T> Result<T> parse(Class<T> t){
		Result<T>result=new Result<T>();
		JsonParser parser = new JsonParser();
		JsonElement jsonEl = parser.parse(json);
		JsonObject jsonObj = null;
		jsonObj = jsonEl.getAsJsonObject();// 转换成Json对象
		int errCode = jsonObj.get("errCode").getAsInt();//
		String errMsg = jsonObj.get("errMsg").getAsString(); 
		result.errCode=errCode;
		result.errMsg=errMsg;
		try{
			T bean= t.newInstance();
	        Method method= t.getMethod("getKeySort");
	        String[] keySort=(String[])method.invoke(bean);
			JsonElement jelRes = jsonObj.get(keySort[0]);//节点
			if (jelRes.isJsonArray()) {// 判断是否为Json数组
				ArrayList<T> list = new ArrayList<T>();
				JsonArray jarr = jelRes.getAsJsonArray();
				Iterator<JsonElement> json = jarr.iterator();
				while (json.hasNext()) {
					T o = gson.fromJson(json.next(), t);
					list.add(o);
				}
				result.t = (T) list;
			}else if(jelRes.isJsonObject()){
				T ojbt=gson.fromJson(jelRes, t);
				result.t=(T) ojbt;
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
}

class Result<T>{
	public int errCode;
	public String errMsg;
	public T t;
}

class Routes{
	public int x;
	public int y;
	public int seq;
	
	@Override
	public String toString() {
		return "Routes [x=" + x + ", y=" + y + ", seq=" + seq + "]";
	}



	public String[] getKeySort(){
		return new String[]{"routes"};
	}
}

